<?php
namespace app\api\controller;

use app\common\controller\Init;
use app\api\model\Folder as FolderModel;
use app\api\model\File as FileModel;
use app\api\model\File;
use think\Db;
use think\Validate;

class Batch extends Init
{

    /**
     * 获取目录列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function batchList() {
        $this->checkRequest('get');
        $folder_id = input('?get.folder') ? input('get.folder') : '';
        $page = input('?get.p') ? input('get.p') : '1';
        $limit = input('?get.limit') ? input('get.limit') : '50';
        $Dir = Db::name('dirs')->where('uid', $this->UID);

        if($folder_id == '') {
            $Dir->where('pdir_id', 0);
        }else{
            $Dir->where('dirid', $folder_id);
        }
        $folder = $Dir->find();

        if(!$folder) {
            error('目录不存在','404');
        }

        $Folder = new FolderModel($this->UID);
        $File = new FileModel($this->UID);
        // 获取当前文件夹下的总文件夹数量
        $all_folder_num = $Folder->getCount($folder['id']);
        $folder_list = $Folder->getList($folder['id'], $page, $limit);

        // 获取当前文件夹下的总文件数量
        $all_file_num = $File->getCount($folder['id']);
        if(count($folder_list) >= $limit) {
            $file_list = [];
        } else {
            $limit = $limit - count($folder_list);
            $file_list = $File->getList($folder['id'], $page, $limit);
        }

        $pdirs = $Folder->getPdirs($folder['id']);
        success([
            'count' => $all_folder_num + $all_file_num,
            'files' => $file_list,
            'folder' => $folder_list,
            'breadcrumb' => $pdirs,
            'zone' => $File->getTotalStorage($this->UID),
            'use_zone' => $File->getUsedStorage($this->UID)
        ]);
    }

    /**
     * 重命名文件（夹）
     * @return mixed
     */
    public function rename() {
        $this->checkRequest('put');
        $PutData = request()->param();
        if (preg_match("[:*?/\" <>|']", $PutData['re_name'])) {
            error('新名称格式不正确',412);
        }

        $validate = new Validate([
//            're_name' => "require|regex:[:*?/\" <>|']",
            'type' => "require|in:folder,file",
        ]);
        $data = [
            're_name' => trim($PutData['re_name']),
            'type' => trim($PutData['type']),
            'id' => trim($PutData['id']),
        ];
        if (!$validate->check($data)) {
            error($validate->getError(),412);
        }
        if($data['id'] < 1) {
            error('保存失败',406);
        }

        if($data['type'] == 'folder') {
            $Folder = new FolderModel($this->UID);
            return $Folder->rename($data['id'],$data['re_name']);
        } else if($data['type'] == 'file') {
            $File = new FileModel($this->UID);
            return $File->rename($data['id'],$data['re_name']);
        }
    }

    public function remove() {
        $this->checkRequest('delete');
        $deleteData = request()->param();
        if(count($deleteData) < 1) {
            error('没有选择数据', 412);
        }

        $Folders = [];
        $Files = [];
        foreach ($deleteData as $item) {
            if($item['type'] == 'dir') {
                $Folders[] = $item['id'];
            } else if($item['type'] == 'file') {
                $Files[] = $item['id'];
            }
        }
        if(count($Folders) > 0) {
            $Folder = new FolderModel($this->UID);
            $FolderRet = $Folder->delete($Folders);
        }

        /*if(count($Files) > 0) {
            $File = new FileModel($this->UID);
            $FileRet = $File->delete($Files);
        }*/

        success('删除成功',200);

    }
}

